﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LINQtoEbay;
using Okl.ET.Web.Ebay.API;
using System.ServiceModel.Channels;
using System.ServiceModel;

namespace Okl.ET.Web.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        //http://cgeers.com/2010/03/30/ebay-developers-program-net-sdk/#top
        public ActionResult Index()
        {
            ViewData["Message"] = "Welcome to ASP.NET MVC!";
            string temp = GetResult();
            return View();
        }

        public ActionResult About()
        {
            return View();
        }

        private static string GetResult()
        {
            string result = string.Empty;
            using (FindingServicePortTypeClient client = new FindingServicePortTypeClient())
            {
                MessageHeader header = MessageHeader.CreateHeader("CustomHeader", "", "");
                using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
                {
                    OperationContext.Current.OutgoingMessageHeaders.Add(header);

                    HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty();
                    httpRequestProperty.Headers.Add("X-EBAY-SOA-MESSAGE-PROTOCOL", "SOAP11");
                    httpRequestProperty.Headers.Add("X-EBAY-SOA-SECURITY-APPNAME", "ebayTrader.okl.com");
                    httpRequestProperty.Headers.Add("X-EBAY-SOA-GLOBAL-ID", "EBAY-GB");
                    httpRequestProperty.Headers.Add("X-EBAY-SOA-OPERATION-NAME", "findItemsByKeywords");

                    OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;

                    FindItemsByKeywordsRequest request = new FindItemsByKeywordsRequest();
                    request.keywords = "canon";
                    FindItemsByKeywordsResponse response = client.findItemsByKeywords(request);
                    foreach (var item in response.searchResult.item)
                    {
                        result += item.title;
                    }

                }


            }

            return result;

        }
    }
}
